package phapp.springSecurity;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.AntPathRequestMatcher;

import phapp.model.system.*;
import phapp.service.system.*;
//import com.jw.system.servlet.FileManagerService;
//import com.jw.system.servlet.systemparameters.SPManagerImpl;
//import com.jw.system.servlet.systemparameters.SParametersHelper;
import org.springframework.web.context.ServletContextAware;

/**
 * <p>
 * title:加载所有的权限配置
 * </p>
 * <p>
 * Description:要实现FilterInvocationSecurityMetadataSource类
 * </p>
 * Copyright: Copyright (c) 2012-10 Company:
 * 
 * @author
 * @version 1.8
 */

public class MyInvocationSecurityMetadataSource implements ServletContextAware, FilterInvocationSecurityMetadataSource
{
   //private ServletContext ctxApp;
   private MenuManager menuManager;
   private AntPathRequestMatcher pathMatcher;
   public static Map<String, Collection<ConfigAttribute>> mapResource = null;
   
   public MyInvocationSecurityMetadataSource()
   {
   }

   public void setMenuManager(MenuManager menuManager)
   {
      this.menuManager = menuManager;
   }
   
   public void setServletContext(ServletContext sctx)
   {
      //ctxApp = sctx;
   }

   public void initData() throws ServletException
   {
      loadResource();
   }

   /**
    * 项目部署时加载所有URL及其对应的权限
    */
   private void loadResource()
   {
      if(mapResource == null)
      {
         mapResource = new HashMap<String, Collection<ConfigAttribute>>();
         
         //List<Menu> listMenu = permissionManager.selectPermissionMenuAndAction();
         List<Menu> listMenu = menuManager.findAll();
         if(listMenu != null)
         {
            for(Menu menuItem : listMenu)
            {
               Collection<ConfigAttribute> listConfigAttribute = new ArrayList<ConfigAttribute>();
               String strMenuID = menuItem.getId().toString();
               if(strMenuID != null)
               {
                  ConfigAttribute configAttribute = new SecurityConfig(strMenuID);
                  listConfigAttribute.add(configAttribute);
                  
                  mapResource.put(menuItem.getFuncUrl(), listConfigAttribute);
               }
            }
         }
      }
   }

   /*
    * (non-Javadoc) 返回用户访问的资源所需的权限信息.object:访问的对象
    * 
    * @see
    * org.springframework.security.access.SecurityMetadataSource#getAttributes
    * (java.lang.Object)
    */
   public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException
   {
      String funcUrl = "";
      Collection<ConfigAttribute> listConfigAttribute = null;
      javax.servlet.http.HttpServletRequest request = ((FilterInvocation) object).getRequest();
      
      if(mapResource == null)   loadResource();
      Iterator<String> iterator = mapResource.keySet().iterator();
      while(iterator.hasNext())
      {
         funcUrl = iterator.next();
         //System.out.println("~~~~~~~~~~~~~~~~~~~~~~funcUrl = "+funcUrl);
         if(funcUrl != null&&funcUrl.length()>2)
         {
            pathMatcher = new AntPathRequestMatcher(funcUrl);
            if(pathMatcher.matches(request))
            {
               listConfigAttribute = mapResource.get(funcUrl);
               return listConfigAttribute;
            }
         }
      }
      
      return null;
   }

   /**
    * 刷新内存，后台权限资源有所更改，调用该static方法进行刷新内存.
    */
   private static void reloadResource()
   {
      if(mapResource != null)   mapResource.clear();
      mapResource = null;
   }

   public boolean supports(Class<?> clazz)
   {
      return true;
   }

   public Collection<ConfigAttribute> getAllConfigAttributes()
   {
      return null;
   }



}
